#include <stdio.h>
#include "./font_data.h"

unsigned int find_adrress(unsigned char *str_ptr);
int main()
{
    unsigned int offset;
    unsigned char test_str[] = {"A中文"};
    unsigned int idx = 0, tmp_offset = 0;
    while (test_str[idx])
    {
        offset = find_adrress(&test_str[idx]);
        if ((test_str[idx] >= 0x20) && (test_str[idx] <= 0x7e))
        {
            printf("%c\t-> offset:0x%0x\n", test_str[idx], offset);
            idx += 1;
            continue;
        }
        if ((test_str[idx] >= 0xc2) && (test_str[idx] <= 0xd1) && (test_str[idx + 1] >= 0x80) && (test_str[idx + 1] <= 0xbf))
        {
            printf("%c%c\t-> offset:0x%0x\n", test_str[idx], test_str[idx+1], offset);
            idx += 2;
            continue;
        }
        if ((test_str[idx] >= 0xe2) && (test_str[idx] <= 0xef)
        && (test_str[idx + 1] >= 0x80) && (test_str[idx + 1] <= 0xbf)
        && (test_str[idx + 2] >= 0x80) && (test_str[idx + 2] <= 0xbf))
        {
            printf("%c%c%c\t-> offset:0x%0x\n", test_str[idx], test_str[idx+1], test_str[idx+2], offset);
            idx += 3;
            continue;
        }
    }
    return 0;
}
unsigned int find_adrress(unsigned char *str_ptr)
{
    unsigned int unicode_tmp;
    unsigned int data_offset, i;
    if (*str_ptr >= 0x20 && *str_ptr <= 0x7E)
    {
        unicode_tmp = *str_ptr;
    }
    if (((*str_ptr) >= 0xC2) && ((*str_ptr) <= 0xD1) &&
        (*(str_ptr + 1) >= 0x80) && (*(str_ptr + 1) <= 0xBF))
    {
        unicode_tmp = ((*str_ptr & 0x1f) << 6) | (*(str_ptr + 1) & 0x3f);
    }
    if (((*str_ptr) >= 0xE2) && ((*str_ptr) <= 0xEF) &&
        (*(str_ptr + 1) >= 0x80) && (*(str_ptr + 1) <= 0xBF) &&
        (*(str_ptr + 2) >= 0x80) && (*(str_ptr + 2) <= 0xBF))
    {
        unicode_tmp = ((*str_ptr & 0xf) << 12) | ((*(str_ptr + 1) & 0x3f) << 6) | (*(str_ptr + 2) & 0x3f);
    }
    for (i = 0; i < MAP_TABLE_LEN; i++)
    {
        if ((unicode_offset_map[i] & 0xffff) == unicode_tmp)
        {
            return (unicode_offset_map[i] >> 16);
        }
    }
}